package com.foreknow.ex.ex8;

import java.util.Date;

/**
 * Created by foreknow on 2021/7/25.
 */
public class Order {
    private String orderId;// 订单的编号
    private Date date;// 订单日期
    private double total;// 订单总额
    private OrderItem[] items;// 保存多个订单项，所购买的每个商品就是一个OrderItem对象（图书的名称，单价，购买数量）

    public Order(String orderId) {
        this.orderId = orderId;
        this.date = new Date();
        this.items = new OrderItem[3];
    }

    public String getOrderId() {
        return orderId;
    }

    public void setOrderId(String orderId) {
        this.orderId = orderId;
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public double getTotal() {
        // 调用calTotal()方法计算订单的总额
        calTotal();
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public OrderItem[] getItems() {
        return items;
    }

    public void setItems(OrderItem[] items) {
        this.items = items;
    }

    /**
     * 将订单项OrderItem对象添加到数组中,i代表数组中的下标
     */
    public void setItem(OrderItem item,int i) {
        this.items[i] = item;
    }


    /**
     * 计算订单的总额=单价*数量
     */
    public void calTotal() {
        double total = 0;
        // 需要对数组进行遍历
        if (items != null) {
            for (int i = 0; i < items.length; i++) {
                total += items[i].getPprice() * items[i].getNum();
            }
        }
        this.total = total;
    }

}
